package org.openkoala.koala.popup;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.util.EclipseUrlParseUtils;
import org.openkoala.koala.widget.Security;

/**
 * 右键项目点击“添加权限管理”时的弹出窗
 * @author xmfang
 *
 */
public class AddSecurityModuleDialog extends Dialog {

	private AddSecurityModuleAction action;
	
	private Security security = new Security();
	private String connectProtocol = "JDBC";
	private String cacheType = "ehCache";
	private String template = "TemplateA";
	
	//控件
	Group modulesGroup;
	Button jdbc;
	Button ejb;
	Button ehCache;
	Button memCached;
	Button templateA;
	Button templateB;
	Button templateC;

	protected AddSecurityModuleDialog(Shell shell, AddSecurityModuleAction action) {
		super(shell);
		this.action = action;
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Add Security Manager");
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);
		
		chooseConnectProtocol(composite);
		chooseCacheType(composite);
		chooseAuthorityTemplate(composite);
		
		return parent;
	}
	
	/**
	 * 显示连接协议供用户选择
	 * @param composite
	 */
	private void chooseConnectProtocol(Composite composite) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;

		Group group = new Group(composite, SWT.NULL);
		group.setText("choose connect protocol");
		group.setLayout(new GridLayout(3, false));
		group.setLayoutData(gridData);

		jdbc = new Button(group, SWT.RADIO);
		jdbc.setLayoutData(gridData);
		jdbc.setText("集成式");
		jdbc.setData("JDBC");
		jdbc.addSelectionListener(new ConnectProtocolSelectionListener());
		jdbc.setSelection(true);

		ejb = new Button(group, SWT.RADIO);
		ejb.setLayoutData(gridData);
		ejb.setText("分布式");
		ejb.setData("EJB");
		ejb.addSelectionListener(new ConnectProtocolSelectionListener());
		
//		ldap.setEnabled(false);
	}
	
	/**
	 * 显示连接协议供用户选择
	 * @param composite
	 */
	private void chooseCacheType(Composite composite) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;

		Group group = new Group(composite, SWT.NULL);
		group.setText("choose cache type");
		group.setLayout(new GridLayout(3, false));
		group.setLayoutData(gridData);

		ehCache = new Button(group, SWT.RADIO);
		ehCache.setLayoutData(gridData);
		ehCache.setText("ehCache");
		ehCache.setData("ehCache");
		ehCache.addSelectionListener(new CacheTypeSelectionListener());
		ehCache.setSelection(true);

		memCached = new Button(group, SWT.RADIO);
		memCached.setLayoutData(gridData);
		memCached.setText("memCached");
		memCached.setData("memCached");
		memCached.addSelectionListener(new CacheTypeSelectionListener());
	}
	
	/**
	 * 显示各个模版类型用户选择生成统一认证系统的模版
	 * @param composite
	 */
	private void chooseAuthorityTemplate(Composite composite) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;

		Group templatesGroup = new Group(composite, SWT.NULL);
		templatesGroup.setText("choose authority template");
		templatesGroup.setLayout(new GridLayout(3, false));
		templatesGroup.setLayoutData(gridData);

		templateA = new Button(templatesGroup, SWT.RADIO);
		templateA.setLayoutData(gridData);
		templateA.setText("TemplateA");
		templateA.setData("TemplateA");
		templateA.addSelectionListener(new TemplateSelectionListener());
		templateA.setSelection(true);

		templateB = new Button(templatesGroup, SWT.RADIO);
		templateB.setLayoutData(gridData);
		templateB.setText("TemplateB");
		templateB.setData("TemplateB");
		templateB.addSelectionListener(new TemplateSelectionListener());

		templateC = new Button(templatesGroup, SWT.RADIO);
		templateC.setLayoutData(gridData);
		templateC.setText("TemplateC");
		templateC.setData("TemplateC");
		templateC.addSelectionListener(new TemplateSelectionListener());

		Label templateAImage = new Label(templatesGroup, SWT.NONE);
		templateAImage.setImage(ImageDescriptor.createFromURL(EclipseUrlParseUtils.parseUrl(getClass().getClassLoader().getResource("org/openkoala/koala/wizard/wizardpage/template1.jpg"))).createImage());
		
		Label templateBImage = new Label(templatesGroup, SWT.NONE);
		templateBImage.setImage(ImageDescriptor.createFromURL(EclipseUrlParseUtils.parseUrl(getClass().getClassLoader().getResource("org/openkoala/koala/wizard/wizardpage/template2.jpg"))).createImage());

		Label templateCImage = new Label(templatesGroup, SWT.NONE);
		templateCImage.setImage(ImageDescriptor.createFromURL(EclipseUrlParseUtils.parseUrl(getClass().getClassLoader().getResource("org/openkoala/koala/wizard/wizardpage/template3.jpg"))).createImage());
	}

	/**
	 * 连接协议选择单选框的监听器
	 * @author xmfang
	 *
	 */
	private class ConnectProtocolSelectionListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			if (radio.getSelection()) {
				connectProtocol = String.valueOf(radio.getData());
			}
		}
	}

	/**
	 * 缓存类型选择单选框的监听器
	 * @author xmfang
	 *
	 */
	private class CacheTypeSelectionListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			if (radio.getSelection()) {
				cacheType = String.valueOf(radio.getData());
			}
		}
	}

	/**
	 * 模版选择单选框的监听器
	 * @author xmfang
	 *
	 */
	private class TemplateSelectionListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent event) {
		}

		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			if (radio.getSelection()) {
				template = String.valueOf(radio.getData());
			}
		}
	}
	
	@Override
	protected void okPressed() {
		security.setConnType(connectProtocol);
		security.setCacheType(cacheType);
		security.setTemplate(template);
		action.AddSecuritySystemToModule(security);
		super.okPressed();
	}
	
}
